package main

import "fmt"

/**
 * @author: guangcheng
 * @date:   2020/11/21 21:04
 * @version  V1.0
 * @description:
 *
 */
func main() {

	num := Number{
		Num1: 10.0,
		Num2: 20.0,
	}

	res := num.Cal('/')
	fmt.Println("res=", res)

	num1 := Number{
		Num1: 100.0,
		Num2: 21.0,
	}
	res1 := num1.Cal('/')
	//res=4.76 实际上是已经格式化好的字符串类型
	fmt.Printf("res=%v", fmt.Sprintf("%.2f", res1))
}

type Number struct {
	Num1 float64
	Num2 float64
}

//编译器简化书写
func (num *Number) Cal(op byte) float64 {
	switch op {
	case '+':
		return num.Num1 + num.Num2
	case '-':
		return num.Num1 - num.Num2
	case '*':
		return num.Num1 * num.Num2
	case '/':
		return num.Num1 / num.Num2
	default:
		return 0
	}
}
